package com.caregiver.watch.business.data.helper;

import com.alibaba.fastjson2.JSON;
import com.caregiver.watch.business.data.protobuf.HisDataOuterClass;
import com.caregiver.watch.business.data.protobuf.HisHealthData;
import com.caregiver.watch.business.data.utils.time.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;

/**
 * @Description: 数据预处理
 * @Author 疆戟
 * @Date 2025/9/14 20:45
 * @Version 1.0
 */
@Slf4j
public final class DataPreprocessorHelper {


    /**
     * 睡眠数据预处理
     */
    public static String sleepPreprocess(HisDataOuterClass.HisData hisData) {
        if (Objects.isNull(hisData)) {
            return StringUtils.EMPTY;
        }
        if (!hisData.hasHealth()) {
            return StringUtils.EMPTY;
        }
        Map<String, Object> sleepMap = new HashMap();
        HisHealthData.HisDataHealth healthData = hisData.getHealth();

        int seq = hisData.getSeq();
        sleepMap.put("Q", seq);

        Date the_data_time = new Date(healthData.getTimeStamp().getDateTime().getSeconds() * 1000L);
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        calendar.setTime(the_data_time);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int times[] = new int[2];
        times[0] = hour;
        times[1] = minute;
        sleepMap.put("T", times);

        String dataTime = DateTimeUtils.convert2DateTimeString(healthData.getTimeStamp().getDateTime().getSeconds());
        if (healthData.hasPedoData()) {
            HisHealthData.HisHealthPedo his_pedo = healthData.getPedoData();
            Map detailMap = new HashMap();
            detailMap.put("s", his_pedo.getStep());
            detailMap.put("c", his_pedo.getCalorie());
            detailMap.put("d", his_pedo.getDistance());
            detailMap.put("t", his_pedo.getType());
            detailMap.put("a", his_pedo.getState() & 15);
            sleepMap.put("P", detailMap);
        }
        if (healthData.hasHrData()) {
            HisHealthData.HisHealthHr his_hr = healthData.getHrData();
            Map detailMap = new HashMap();
            if (his_hr.getAvgBpm() > 0) {
                detailMap.put("a", his_hr.getAvgBpm());
            }
            if (his_hr.getMaxBpm() > 0) {
                detailMap.put("x", his_hr.getMaxBpm());
            }
            if (his_hr.getMinBpm() > 0) {
                detailMap.put("n", his_hr.getMinBpm());
            }
            sleepMap.put("H", detailMap);
        }
        if (healthData.hasHrvData()) {
            HisHealthData.HisHealthHrv his_hrv = healthData.getHrvData();
            Map detailMap = new HashMap();
            if (his_hrv.getSDNN() > 0) {
                detailMap.put("s", his_hrv.getSDNN() / 10.0);
            }
            if (his_hrv.getRMSSD() > 0) {
                detailMap.put("r", his_hrv.getRMSSD() / 10.0);
            }
            if (his_hrv.getPNN50() > 0) {
                detailMap.put("p", his_hrv.getPNN50() / 10.0);
            }
            if (his_hrv.getMEAN() > 0) {
                detailMap.put("m", his_hrv.getMEAN() / 10.0);
            }
            int fatigue = (int) his_hrv.getFatigue();
            if (fatigue > -1000) {
                if (fatigue <= 0) {
                    fatigue = (int) (Math.log((double) his_hrv.getRMSSD()) * 20);
                }
                if (fatigue > 0) {
                    detailMap.put("f", fatigue);
                }
                if (!detailMap.isEmpty()) {
                    sleepMap.put("V", detailMap);
                }
            }
        }
        if (healthData.hasSleepData()) {
            HisHealthData.HisHealthSleep sleepData = healthData.getSleepData();
            Map detailMap = new HashMap();
            detailMap.put("a", sleepData.getSleepDataList());
            if (sleepData.hasShutDown()) {
                detailMap.put("s", sleepData.getShutDown());
            }
            if (sleepData.hasCharge()) {
                detailMap.put("c", sleepData.getCharge());
            }
            sleepMap.put("E", detailMap);
        }
                /*
                    save sleep_str with data_time/seq, later to do sleep calculation,
                    need combine all day's sleep_str
                 */
        String sleepStr = JSON.toJSONString(sleepMap);
        log.info("dataTime:{} seq:{} sleepStr:{}", dataTime, seq, sleepStr);
        return sleepStr;
    }
}
